<template>
  <div class="app-container">
    <!-- 空载车辆确保 -->
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          size="mini"
          label-width="88px"
        >
          <el-form-item label="车牌号" prop="transportName">
            <el-input
              v-model.trim="queryParams.transportName"
              placeholder="请输入车牌号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="运输批次号" prop="manifestId">
            <el-input
              v-model.trim="queryParams.manifestId"
              placeholder="请输入运输批次号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="进出口" prop="iEFlag">
            <el-select
              v-model.trim="queryParams.iEFlag"
              placeholder="请输入进出口类型"
              clearable
            >
              <el-option v-for="(item, index) in dict.type.mft_ieport_type"
                         :key="index"
                         :label="item.label"
                         :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model.trim="queryParams.status"
              placeholder="请选择状态"
              clearable
            >
              <el-option v-for="(item, index) in dict.type.mft_head_status"
                         :key="index"
                         :label="item.label"
                         :value="item.value"/>
            </el-select>
          </el-form-item>

          <el-form-item label="确报类型" prop="bindingtype">
            <el-select
              v-model.trim="queryParams.bindingtype"
              placeholder="请选择确报类型"
              clearable
            >
              <el-option
                v-for="(item, index) in dict.type.mft_binding_type"
                :key="index"
                :label="item.value+'-'+item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleEmptyAddEdit('add')"
            v-hasPermi="['swdecl:swBdHead:add']"
          >新增
          </el-button
          >
        </el-col>
       <!-- <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleHeavyAddEdit('add')"
            v-hasPermi="['swdecl:swBdHead:add']"
          >新增载货车辆确报
          </el-button
          >
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['swdecl:swBdHead:remove']"
          >删除
          </el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
            :disabled="multiple"
            size="mini"
            @click="handleDeclQB"
            v-hasPermi="['swdecl:swBdHead:decl']"
          >申报
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
            :disabled="multiple"
            size="mini"
            @click="handleDeleteDecl"
            v-hasPermi="['swdecl:swBdHead:decl']"
          >删除申报
          </el-button>
            <el-button
              type="primary"
              plain
              icon="el-icon-upload"
              :disabled="multiple"
              size="mini"
              @click="handleGenBill"
              v-hasPermi="['swdecl:swBdHead:genGbill']"
            >生成单证
            </el-button>
        </el-col>
        <el-col :span="1.5">
          <!-- 查看回执 -->
          <view-response :disabled="single" :msgType="mType" :checkedId="manifestIdList"></view-response>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <!-- 列表 -->
      <basic-table
        ref="tableList"
        :columns="columns"
        :tableData="tableData"
        :total="pageParams.total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        :loading="loading"
        :needCheckbox="true"
        @selectionChange="handleSelectionChange"
      >
        <template #iEFlag="scope">
          <dict-tag :options="dict.type.sys_i_e_flag" :value="scope.row.iEFlag"></dict-tag>
        </template>
        <template #bindingType="scope">
          <dict-tag :options="dict.type.mft_binding_type" :value="scope.row.bindingType"></dict-tag>
        </template>
        <template #declStatus="scope">
          <dict-tag :options="dict.type.mft_bd_decl_status" :value="scope.row.declStatus"></dict-tag>
        </template>

        <template #rcptStatus="scope">
          <dict-tag :options="dict.type.mft_response_code" :value="scope.row.rcptStatus"></dict-tag>
        </template>
        <template #customsStateCode="scope">
          <dict-tag :options="dict.type.mft_biz_mode" :value="scope.row.customsStateCode"></dict-tag>
        </template>
        <template #genBillMark="scope">
          <dict-tag :options="dict.type.mftbd_gen_bills_mark" :value="scope.row.genBillMark"></dict-tag>
        </template>

        <template #customsCode="scope">
          {{ scope.row.customsCode }}-
          <dict-value :list="customsList" :dictCode="scope.row.customsCode" label="label"
                      value="value"></dict-value>
        </template>

        <template #operate="scope">
          <div class="option-menu">
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleAddEdit('edit', scope.row)"
              v-hasPermi="['bt:gtUnifiedDeclaration:bdAdd']"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              size="mini"
              class="table-edit"
              @click="handleAddEdit('detail', scope.row)"
            >
              详情
            </el-button>
          </div>
        </template>
      </basic-table>
    </basic-container>

    <!-- 新增、编辑 -->
    <add-edit-heavy
      ref="addEditHeavy"
      :dialogType="dialogType"
      :rowData="rowData"
      :dict="dictProp"
      @handleQuery="handleQuery"
    />

    <add-edit-empty
      ref="addEditEmpty"
      :dialogType="dialogType"
      :rowData="rowData"
      :dict="dictProp"
      @handleQuery="handleQuery"
    />

    <!-- 详情 -->
    <!-- <detail-dialog ref="detail" :detailId="detailId"></detail-dialog> -->
  </div>
</template>

<script>
import {
 listSwBdHead,
 delSwBdHead,
 genGbill,
 declSwBdHead,
 declRepeatSwBdHead,
 chargebackSwBdHead
} from "@/api/swdecl/swBdHead";
import addEditEmpty from "./components/addEditEmpty";
import addEditHeavy from "./components/addEditHeavy";
   import {findListCustoms} from "@/api/system/customs";
import ViewResponse from '../components/viewResponse.vue';

export default {
  name: "Hs-vehicle-declaration",
  components: {
    addEditEmpty,
    addEditHeavy,
    ViewResponse,
  },
   dicts: ["sys_i_e_flag", 'mft_decl_status', 'mft_bd_decl_status', 'mft_bill_decl_status', 'mft_binding_type','sys_bill_audit_status','mft_biz_mode','mftbd_gen_bills_mark','mft_response_code'],
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      mType:'MT_44',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        transportName: null,
        unifiedCode: null,
        manifestId: null,
        iEFlag: null,
        customsStateCode: null,
        agentEntCode: null,
        customsCode: null,
        loadTime: null,
        unloadCode: null,
        loadCode: null,
        customMasterCode: null,
        arrivalDate: null,
        status: null,
        msgRepCode: null,
        msgRepName: null,
        startFieldCode: null,
        startFieldName: null,
        carrierCode: null,
        bindingtype: null,
      },
      dialogType: "add",
      // 遮罩层
      loading: true,
      // 列表数据
      tableData: [],
      columns: [
        {
          prop: "manifestId",
          label: "运输批次号",
          minWidth: 140,
        },
        {
          prop: "iEFlag",
          label: "进出口类型",
          align: "center",
          minWidth: 100,
          slot: true
        },
        {
          prop: "customsCode",
          label: "进出境口岸",
          align: "center",
          slot: true,
          minWidth: 140,
        },
        {
          prop: "bindingType",
          label: "确报类型",
          align: "center",
          slot: true,
          minWidth: 240
        },
        {
          prop: "customsStateCode",
          label: "通关代码",
          align: "center",
          slot: true,
          minWidth: 140,
        },
        {
          prop: "declStatus",
          label: "申报状态",
          align: "center",
          minWidth: 120,
          slot: true
        },
        {
          prop: "rcptStatus",
          label: "回执状态",
          align: "center",
          minWidth: 120,
          slot: true
        },
        {
          prop: "rcptRemark",
          label: "回执说明",
          align: "center",
          minWidth: 120,
        },
        {
          prop: "genBillMark",
          label: "单证状态",
          align: "center",
          minWidth: 120,
          slot: true
        },
        {
          prop: "msgRepName",
          label: "申报企业",
          minWidth: 180
        },
        {
          prop: "createBy",
          label: "创建人",
          align: "center",
        },
        {
          prop: "createTime",
          label: "创建时间",
          align: "center",
          width: 160,
        },
        {
          prop: "operate",
          label: "操作",
          slot: true,
          width: 140,
          fixed: "right",
        },
      ],
      pageParams: {
        total: 0,
      },
      rowData: {},
      detailId: "",
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 选中的批次号
      manifestIdList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //详情显示的时候，表单编辑禁用
      isViewDisabled: false,

      // 舱单申报头表格数据
      mftManifestHeadList: [],
      customsList: [],
    };
  },
  computed: {
    dictProp() {  //字典list，为避免子组件重复调接口获取，用computed计算传值
      return {
        type: this.dict.type
      }
    }
  },
  created() {
    this.getList();
    this.loadCustomsList();
  },
  methods: {
    /** 查询舱单申报头列表 */
    getList() {
      this.loading = true;
      listSwBdHead(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.pageParams.total = response.total;
        this.loading = false;
      });
    },
    /* 加载关区信息列表*/
    loadCustomsList() {
      findListCustoms().then(response => {
        this.customsList = response.rows;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$refs.tableList.toPageFirst();
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 分页
    handleSizeChange(val) {
      this.queryParams.pageSize = val.pageSize;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val.currentPage;
      this.getList();
    },
    // 新增/编辑
    handleHeavyAddEdit(type, row) {
      this.dialogType = type;
      if (row) {
        this.rowData = JSON.parse(JSON.stringify(row));
      }
      this.$refs.addEditHeavy.open();
    },
    handleEmptyAddEdit(type, row) {
      this.dialogType = type;
      if (row) {
        this.rowData = JSON.parse(JSON.stringify(row));
      }
      this.$refs.addEditEmpty.open();
    },

    // 详情
    handleDetail(row) {
      this.detailId = row.id;
      this.$nextTick(() => {
        this.$refs.detail.open();
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.manifestIdList = selection.map((item) => item.manifestId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除舱单申报头编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSwBdHead(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    //确保申报
    handleDeclQB(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要确保申报的单证数据！");
        return false;
      }
      this.$modal.confirm('是否确认申报业务编号为"' + billCodes + '"的数据项？').then(function () {
        return declSwBdHead(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申报成功");
      }).catch(() => {
      });
    },
    //删除申报
    handleDeleteDecl(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要删除申报的单证数据！");
        return false;
      }
      this.$modal.confirm('是否确认删除业务编号为"' + billCodes + '"的申报数据项？').then(function () {
        return chargebackSwBdHead(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除申报操作成功");
      }).catch(() => {
      });
    },
    handleGenBill(row) {
      const billCodes = row.id || this.ids;
      if (billCodes == undefined || billCodes == null || billCodes == "") {
        this.$modal.alertError("请先选择要生成的单证数据！");
        return false;
      }
      this.$modal.confirm('是否确认生成业务编号为"' + billCodes + '"的申报数据项？').then(function () {
        return genGbill(billCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("生成操作成功");
      }).catch(() => {
      });
    },

  },
};
</script>


<style type="text/css">
.el-form-item .el-select {
  width: 100%;
}
</style>
